﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.Forms
{
    public partial class TaskForm : RelationsLMP.Control.Form.MetroDemoForms
    {
        private UC.Alarm.AlarmManagement AlarmManagement;
        List<string> ListFactoryNumber;
        /// <summary>
        /// 任务累计序号
        /// </summary>
        int TaskCumulativeNumber;

        public Model.DB.tb_TaskInfo TaskInfo { get; set; }

        public TaskForm()
        {
            InitializeComponent();

            ListFactoryNumber = new List<string>();
            TaskCumulativeNumber = Static.ParaInfo.TaskCumulativeNumber + 1;

            TaskInfo = new Model.DB.tb_TaskInfo();

            InitControls();
        }

        public void ModifyInfo(Model.DB.tb_TaskInfo _Temp)
        {
            TaskInfo.Id = _Temp.Id;

            ListFactoryNumber.Clear();
            ListFactoryNumber.AddRange(_Temp.TaskDetails.Select(x => x.FactoryNumber).ToList());
            AlarmManagement_Event_AddAlarmInstrumentInfo(null);

            cmbUser.SelectedIndexChanged -= CmbUser_SelectedIndexChanged;
            cmbUser.SelectedValue = _Temp.UserId;
            cmbUser.Enabled = false;

            labID.Text = _Temp.Id.ToString();
            txtTitle.Text = _Temp.Title.ToString();
            txtDescription.Text = _Temp.Desc.ToString();
        }

        private void InitControls()
        {
            InitComBoxUserInfo();

            Static.Methods.CreateDGVColumns(ref dgvAlarmSimple, Model.SystemEnum.DGVType.AlarmSSType);
            dgvAlarmSimple.Columns["Selected"].Visible = false;
            dgvAlarmSimple.Columns["SubmitUnit"].Visible = false;
            dgvAlarmSimple.Columns["sSampingMode"].Visible = false;
            dgvAlarmSimple.CellClick += DgvAlarmSimple_CellClick;
            
            AlarmManagement = new UC.Alarm.AlarmManagement();
            AlarmManagement.PlugInCall("Task");
            AlarmManagement.Event_AddAlarmInstrumentInfo += AlarmManagement_Event_AddAlarmInstrumentInfo;
            AlarmManagement.Dock = DockStyle.Fill;
            pAlarmManagement.Controls.Add(AlarmManagement);

            okReturnToolbar21.Event_OKClick += OkReturnToolbar21_Event_OKClick;
            okReturnToolbar21.Event_ReturnClick += OkReturnToolbar21_Event_ReturnClick;
        }

        private void DgvAlarmSimple_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || !dgvAlarmSimple.Columns[e.ColumnIndex].Name.Equals("Delete")) return;
            
            if (MessageBox.Show(this, string.Format("确定要删除此报警器吗？"), "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                ListFactoryNumber.Remove(dgvAlarmSimple.Rows[e.RowIndex].Cells["FactoryNumber"].Value.ToString());
                dgvAlarmSimple.Rows.RemoveAt(e.RowIndex);
            }
        }

        private void OkReturnToolbar21_Event_OKClick(object sender, EventArgs e)
        {
            TaskInfo.Id = labID.Text;

            if (string.IsNullOrEmpty(txtTitle.Text))
            {
                MessageBox.Show(this, "【标题】不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTitle.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtDescription.Text))
            {
                MessageBox.Show(this, "【描述】不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDescription.Focus();
                return;
            }

            TaskInfo.Title = txtTitle.Text;
            TaskInfo.Desc = txtDescription.Text;
            TaskInfo.UserId = (int)cmbUser.SelectedValue;

            TaskInfo.TaskDetails.Clear();
            foreach (var item in ListFactoryNumber)
            {
                TaskInfo.TaskDetails.Add(new Model.DB.tb_TaskDetails()
                {
                    TaskId = labID.Text,
                    FactoryNumber = item
                });
            }

            this.DialogResult = DialogResult.OK;
        }

        private void OkReturnToolbar21_Event_ReturnClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void AlarmManagement_Event_AddAlarmInstrumentInfo(List<string> list)
        {
            dgvAlarmSimple.Rows.Clear();

            List<Model.DB.tb_AlarminstrumentInfo> _ListAlarmInstrumentInfo;

            if (list != null)
            {
                ListFactoryNumber.AddRange(list);
            }

            if (ListFactoryNumber.Count < 0) return;

            ListFactoryNumber = ListFactoryNumber.Distinct().ToList();

            _ListAlarmInstrumentInfo = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentForFactoryNumberMore(ListFactoryNumber);

            //dgvAlarmSimple.DataSource = _ListAlarmInstrumentInfo;
            for (int i = 0; i < _ListAlarmInstrumentInfo.Count; i++)
            {
                dgvAlarmSimple.Rows.Add();
                dgvAlarmSimple.Rows[i].Cells["InstrumentId"].Value = _ListAlarmInstrumentInfo[i].InstrumentId;
                dgvAlarmSimple.Rows[i].Cells["InstrumentName"].Value = _ListAlarmInstrumentInfo[i].InstrumentName;
                dgvAlarmSimple.Rows[i].Cells["FactoryNumber"].Value = _ListAlarmInstrumentInfo[i].FactoryNumber;
                dgvAlarmSimple.Rows[i].Cells["SubmitUnit"].Value = _ListAlarmInstrumentInfo[i].SubmitUnit;
                dgvAlarmSimple.Rows[i].Cells["sGasTypeList"].Value = _ListAlarmInstrumentInfo[i].sGasTypeList;
                //dgvAlarmSimple.Rows[i].Cells["sSampingMode"].Value = _ListAlarmInstrumentInfo[i].SampingMode;
            }
        }

        private void InitComBoxUserInfo()
        {

            Static.Methods.InitComBox(ref cmbUser , Model.SystemEnum.ComBoxType.UserInfo);

            cmbUser.SelectedIndexChanged += CmbUser_SelectedIndexChanged;

            if (cmbUser.Items.Count > 0)
            {
                cmbUser.SelectedIndex = 0;

                labID.Text = DateTime.Now.ToString("yyyMMdd") + cmbUser.SelectedValue.ToString().PadLeft(4, '0') + TaskCumulativeNumber.ToString().PadLeft(6, '0');
            }
        }

        private void CmbUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            labID.Text = DateTime.Now.ToString("yyyMMdd") + cmbUser.SelectedValue.ToString().PadLeft(4, '0') + TaskCumulativeNumber.ToString().PadLeft(6, '0');
        }
    }
}
